import {
    request
} from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({
    /**
     * 页面的初始数据
     */
    data: {
        loading: false,
        contact: '',
        contant: ''
    },

    async formSubmit(e) {
        let _that = this;
        let content = e.detail.value.opinion;
        let contact = e.detail.value.contact;
        let regPhone = /^1[3578]\d{9}$/;
        let regEmail = /^[a-z\d_\-\.]+@[a-z\d_\-]+\.[a-z\d_\-]+$/i;
        if (content == "") {
            wx.showModal({
                title: '提示',
                content: '反馈内容不能为空！',
            })
            return false
        }
        if (contact == "") {
            wx.showModal({
                title: '提示',
                content: '手机号或者邮箱不能为空！',
            })
            return false
        }
        if (contact == "" && content == "") {
            wx.showModal({
                title: '提示',
                content: '反馈内容，手机号或者邮箱不能为空！',
            })
            return false
        }
        if ((!regPhone.test(contact) && !regEmail.test(contact)) || (regPhone.test(contact) && regEmail.test(contact))) { //验证手机号或者邮箱的其中一个对
            wx.showModal({
                title: '提示',
                content: '您输入的手机号或者邮箱有误!',
            })
            return false
        } else {
            this.setData({
                loading: true
            })
            let model, system, platform;
            wx.getSystemInfo({
                success: function (res) {
                    model = res.model; 
                    system = res.system;
                    platform = res.platform;
                }
            })
            const res = await request({
                url: "/feedback/feedback",
                method: "POST",
                data: {
                    'content': content,
                    'contact': contact,
                    'deviceModel': model, //手机型号
                    'deviceSystem ': system, //操作系统版本
                    'appVersion': platform //客户端平台
                },
            });
            if (200 === res.data.code) {
                _that.setData({
                    loading: false,
                    contact: '',
                    contant: ''
                })
                wx.showToast({
                    title: '成功',
                    icon: 'success',
                    duration: 1500
                })
                setTimeout(function() {
                    wx.switchTab({
                        url: '/pages/ucenter/index',
                      })
                }, 1500)
            } else {
                wx.showToast({
                    title: '系统异常！',
                    icon: 'error',
                    duration: 1500
                })
                console.log("意见反馈接口调用失败")
            }
        }
    },
})